package com.machine.filling.function.main.cardInfoFg;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.function.main.base.BaseFgFragment;
import com.machine.filling.function.main.topFg.TopFgFragment;
import com.machine.filling.net.serialPort.receive.Receive_PrepareFueling;
import com.machine.filling.net.ws.response.WsResCardInfoBean;
import com.machine.filling.util.computer.ArithUtil;

/**
 * Created by Administrator on 2018/2/1 0001.
 * 油卡信息界面
 */

public class CardInfoFgFragment extends BaseFgFragment<CardInfoFgContract.Presenter> implements CardInfoFgContract.View, View.OnClickListener {


    private TextView tv_name;
    private TextView tv_balance;
    private TextView tv_cardlevel;
    private TextView tv_card_type;
    private TextView tv_back_time;
    private TextView tv_bottom;
    private ImageView iv_back;
    private String portNum;
    private String gunId;
    private String cardNum;
    private String encryCarNum;
    private Receive_PrepareFueling cmd_prepare;
    private long BACK_TIME;//返回主界面的倒计时s
    private static final int timeBack = 10;

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_fg_cardinfo, null);
        tv_name = (TextView) view.findViewById(R.id.tv_name);
        tv_balance = (TextView) view.findViewById(R.id.tv_balance);

        tv_cardlevel = (TextView) view.findViewById(R.id.tv_cardlevel);
        tv_card_type = (TextView) view.findViewById(R.id.tv_card_type);
        tv_bottom = (TextView) view.findViewById(R.id.tv_bottom);
        iv_back = (ImageView) view.findViewById(R.id.iv_back);
        tv_back_time = (TextView) view.findViewById(R.id.tv_back_time);
        iv_back.setOnClickListener(this);
        return view;
    }

    @Override
    public void initDatas() {
        tv_bottom.setText("卡信息获取中...");
        mPresenter = new CardInfoPresenter(mContext, this);
        //串口获取到插卡指令， 开始获取卡信息
        mPresenter.getCardInfo();
    }

    public void setCardInfo(String portNum, String gunId, String cardNum, String encryCarNum, Receive_PrepareFueling cmd_prepare) {
        this.portNum = portNum;
        this.gunId = gunId;
        this.cardNum = cardNum;
        this.encryCarNum = encryCarNum;
        this.cmd_prepare = cmd_prepare;
    }

    @Override
    public String getGunId() {
        return gunId;
    }

    @Override
    public String getCardNum() {
        return cardNum;
    }

    @Override
    public String getEncryCarNum() {
        return encryCarNum;
    }

    /**
     * 显示油卡信息
     * 用户卡只能 云端扣费
     * 员工卡,可以使用 微信 支付宝 现金
     *
     * @param cardBean
     */
    @Override
    public void showCardInfoResult(WsResCardInfoBean cardBean) {
        tv_name.setText(cardBean.getName());
        tv_balance.setText(cardBean.getBalance() + "元");
        String cardDiscount = "1".equalsIgnoreCase(cardBean.getCardDiscount()) ? "" : "(优惠:" + cardBean.getCardDiscount() + "元/升)";
        tv_cardlevel.setText(cardBean.getCardLevel() + cardDiscount);
        switch (cardBean.getType()) {    //1用户的加油卡 2员工卡
            case "1":
                tv_card_type.setText("客存卡");
                break;
            case "2":
                tv_card_type.setText("员工卡");
                tv_cardlevel.setText("");
                break;
            default:
                tv_card_type.setText("未知卡类型");
                tv_cardlevel.setText("");
                break;
        }
        tv_bottom.setText("请设置金额或抬枪加油");
        cardInfoReceiveListener.httpCardInfoResult(cardBean);
    }

    @Override
    public void showCardInfoErrorResult(String errorCode, String errorMsg) {
        tv_bottom.setText(errorMsg);
        BACK_TIME = timeBack;
        tv_back_time.setText(BACK_TIME + "s");
        handler.sendEmptyMessageDelayed(1001, 1 * 1000);//每隔1秒进行一次发消息更新界面

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                INextFragment.replaceNext(new Intent(), TopFgFragment.class.getName());
                handler.removeMessages(1001);//取消之前所有循环
                break;
        }
    }

    public void setCardInfoReceiveListener(HttpCardInfoReceiveListener cardInfoReceiveListener) {
        this.cardInfoReceiveListener = cardInfoReceiveListener;
    }

    private HttpCardInfoReceiveListener cardInfoReceiveListener;

    public interface HttpCardInfoReceiveListener {
        void httpCardInfoResult(WsResCardInfoBean cardInfoBean);
    }


    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1001:
                    BACK_TIME--;
                    tv_back_time.setText(BACK_TIME + "s");
                    if (BACK_TIME <= 0) {
                        mPresenter.port_sendCardInfo_1101_errorCard(portNum, cmd_prepare);
                        INextFragment.replaceNext(new Intent(), TopFgFragment.class.getName());
                    } else {
                        handler.sendEmptyMessageDelayed(1001, 1 * 1000);
                    }
                    break;
            }
        }
    };

    @Override
    public void onPause() {
        super.onPause();
        handler.removeMessages(1001);//取消之前所有循环
    }
}
